package com.it.boot.util;

import java.util.*;
import java.lang.reflect.Array;

public class BoolUtils {
    public static boolean toBool(Object target) {
        if (target == null) return false;
        if (target instanceof Boolean) return (Boolean) target;
        if (target instanceof CharSequence) return ((CharSequence) target).length() > 0;
        if (target instanceof Number) return ((Number) target).doubleValue() != 0d;
        if (target instanceof Collection) return !((Collection<?>) target).isEmpty();
        if (target instanceof Map) return !((Map<?, ?>) target).isEmpty();
        if (target.getClass().isArray()) return Array.getLength(target) > 0;
        return true; // 其他非null对象视为true
    }

    // AND 操作
    public static boolean all(Object... targets) {
        for (Object o : targets) {
            if (!toBool(o)) return false;
        }
        return targets.length > 0; // 空参数应返回？
    }

    // OR 操作
    public static boolean any(Object... targets) {
        for (Object o : targets) {
            if (toBool(o)) return true;
        }
        return false;
    }

    public static void main(String[] args) {
        System.out.println(toBool(0.0));
        System.out.println(toBool(""));
        System.out.println(toBool(false));
        System.out.println(toBool(new ArrayList<>()));
        System.out.println(toBool(new Object()));
    }
}
